package com.adnaan.backend.controller.admin;

import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.PageParams;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.SysPermission;
import com.adnaan.backend.entity.dto.PermissionTree;
import com.adnaan.backend.entity.vo.RoleAuthority;
import com.adnaan.backend.service.SysPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * author: adnaan
 * create: 2025-01-12 19:42
 * Description:
 */
@Tag(name = "权限表")
@RestControllerMapping("/sysPermission")
@ApiSupport(author = "adnaan", order = 4)
public class SysPermissionController {

    @Resource
    private SysPermissionService sysPermissionService;

    @PostMapping("/page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页查询")
    public Result<Page<SysPermission>> pageQuery(@RequestBody PageParams<SysPermission> pageParams) {
        // 1.查询参数
        SysPermission params = pageParams.getParams();
        LambdaQueryWrapper<SysPermission> lqw = new LambdaQueryWrapper<>();
        lqw.eq(params.getType() != null, SysPermission::getType, params.getType());
        // 2.查询条件
        lqw.like(params.getName() != null, SysPermission::getName, params.getName());
        lqw.like(params.getNameEn() != null, SysPermission::getNameEn, params.getNameEn());
        lqw.eq(params.getCode() != null, SysPermission::getCode, params.getCode());
        lqw.or().isNull(SysPermission::getParentId);// 全局权限指令返回
        lqw.orderByAsc(SysPermission::getSort);
        // 3.分页查询
        Page<SysPermission> page = sysPermissionService.page(pageParams.getPage(), lqw);
        // 4.处理查询结果，父子关系
        if (params.isTree()) {
            sysPermissionService.tree(page.getRecords());
        }
        return Result.success(page);
    }

    @GetMapping("/tree")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "查询所有数据")
    public Result<List<PermissionTree>> tree() {
        // 查询所有数据
        List<SysPermission> list = sysPermissionService.list();// 查询所有数据
        // 处理父子关系
        List<PermissionTree> permissionTrees = sysPermissionService.permissionTree(list);
        return Result.success(permissionTrees);
    }

    @PostMapping("/insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增数据")
    public Result<Boolean> insert(@RequestBody SysPermission sysPermission) {
        if (sysPermissionService.save(sysPermission)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新数据")
    public Result<Boolean> update(@RequestBody SysPermission sysPermission) {
        if (sysPermissionService.updateById(sysPermission)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除数据")
    public Result<Boolean> deleteById(@PathVariable String id) {
        if (sysPermissionService.removeById(id)) {

            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/getPermissionByRoleId/{roleId}")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "通过角色id查询权限")
    public Result<List<Long>> getPermissionByRoleId(@PathVariable Long roleId) {
        List<Long> list = sysPermissionService.getPermissionByRoleId(roleId);
        return Result.success(list);
    }

    @PostMapping("/assignPermission")
    @ApiOperationSupport(order = 6)
    @Operation(summary = "分配权限")
    public Result<Boolean> assignPermission(@RequestBody RoleAuthority roleAuthority) {
        if (sysPermissionService.assignPermission(roleAuthority)) {
            return Result.success(true);
        }
        return Result.error();
    }
}